package com.example.opposdka.nearme.loginandregister;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.fengfeng.PlatformCallback;
import com.fengfeng.channel.PlatformSDK;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PlatformSDK.getInstance().onCreate(this);
    }

    public void initOnClick(View view) {
        PlatformSDK.getInstance().init(new PlatformCallback() {
            @Override
            public void initSuccess() {
                showToast("初始化成功");
            }

            @Override
            public void initFail(int code, String msg) {
                showToast("初始化失败:" + msg);
            }

            @Override
            public void loginSuccess(String uid) {
                showToast("登录成功:uid为" + uid);
            }

            @Override
            public void loginFail(int code, String msg) {
                showToast("登录失败:" + msg);
            }

            @Override
            public void logoutSuccess() {
                showToast("登出账号成功");
                //重新拉起登录
                PlatformSDK.getInstance().login();
            }

            @Override
            public void logoutFail(int code, String msg) {
                showToast("登出账号失败");
            }
        });
    }

    public void loginOnClick(View view) {
        PlatformSDK.getInstance().login();
    }

    public void logoutOnClick(View view) {
        PlatformSDK.getInstance().logout();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        PlatformSDK.getInstance().onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        PlatformSDK.getInstance().onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PlatformSDK.getInstance().onDestroy();
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onBackPressed() {
        PlatformSDK.getInstance().onBackPressed();
    }

    private void showToast(Object object) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, object.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}